<%@page contentType="text/html" %>
<%@page pageEncoding="UTF-8" %>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@taglib prefix="a4j" uri="http://richfaces.org/a4j" %>
<%@taglib prefix="rich" uri="http://richfaces.org/rich" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Goodwill - Список заказов</title>
    <link rel="stylesheet" href="resources/css/main.css"/>
    <link rel="stylesheet" href="resources/css/stylesheet.css"/>
    <script type="text/javascript" src="resources/js/goodwill.js"></script>
    <style type="text/css">
        #calBegdateInputDate {
            margin-left: 6px;
            width: 80px;
        }

        #calEnddateInputDate {
            margin-left: 6px;
            width: 80px;
        }
    </style>
    <script type="text/javascript">
        function editOrder(orderId, clientId) {
            _editOrder(orderId, clientId);
        }

        function deleteOrder(orderId) {
            if (confirm('Вы уверены, что хотите удалить заказ №' + orderId)) {
                _deleteOrder(orderId);
            }
        }
    </script>
</head>
<body>
<f:view>

<jsp:include page="menu.jsp"/>
<jsp:include page="status.jsp"/>
<jsp:include page="messages.jsp"/>

<a4j:form id="jsForm" prependId="false">
    <a4j:jsFunction name="resetClientName" ajaxSingle="true" limitToList="true" action="#{OrdersBean.resetClientName}"
                    reRender="dvFilter"/>

    <a4j:jsFunction name="_editOrder" ajaxSingle="true" action="#{SessionBean.getOrderItems}" oncomplete="go('order')">
        <a4j:actionparam name="order_id" assignTo="#{SessionBean.selectedOrderId}"/>
        <a4j:actionparam name="client_id" assignTo="#{SessionBean.selectedClientId}"/>
    </a4j:jsFunction>

    <a4j:jsFunction name="_deleteOrder" ajaxSingle="true" action="#{OrdersBean.deleteOrder}" reRender="tblOrders">
        <a4j:actionparam name="order_id" assignTo="#{OrdersBean.orderId}"/>
    </a4j:jsFunction>
</a4j:form>


<%-- Фильтр --%>
<h:panelGroup id="dvFilter" styleClass="" layout="block"
              style="vertical-align:middle;background:whitesmoke;padding:5px">
    <h:form>
        <h:outputText styleClass="b" value="Поиск заказов: "/>

        <h:panelGrid cellpadding="0" cellspacing="0" columns="2">
            <h:panelGroup styleClass="" layout="block">
                <h:panelGrid cellpadding="0" cellspacing="0" columns="6">

                    <h:outputText value="Менеджер" style="margin-left:12px;"/>
                    <h:panelGroup style="margin-left:6px;">
                        <h:panelGroup rendered="#{SecurityBean.isRoleAdministrator}">
                            <h:selectOneMenu value="#{SessionBean.ordersFilter.managerId}" styleClass="f_s"
                                             style="width:200px">
                                <f:selectItems value="#{OrdersBean.managersList}"/>
                            </h:selectOneMenu>
                        </h:panelGroup>
                        <h:panelGroup rendered="#{SecurityBean.isRoleManager}">
                            <h:outputText value="#{SecurityBean.currentUser.fio}"/>
                        </h:panelGroup>
                    </h:panelGroup>

                    <h:outputText value="Период" style="margin-left:12px;"/>
                    <h:panelGrid columns="4" cellpadding="0" cellspacing="5">
                        <h:outputText value=" c "/>
                        <rich:calendar value="#{SessionBean.ordersFilter.begDateAsJavaUtil}" locale="RU_ru"
                                       datePattern="dd.MM.yyyy" style="width:80px;padding-left:4px;" id="calBegdate"/>
                        <h:outputText value=" по "/>
                        <rich:calendar value="#{SessionBean.ordersFilter.endDateAsJavaUtil}" locale="RU_ru"
                                       datePattern="dd.MM.yyyy" style="width:80px;padding-left:4px;" id="calEnddate"/>
                    </h:panelGrid>

                    <h:outputText value="Номер заказа в 1С" style="margin-left:12px;"/>
                    <h:inputText value="#{SessionBean.ordersFilter.c1Num}" styleClass="f_tb_l"
                                 style="margin-left:6px;"/>

                    <h:outputText value="Статус" style="margin-left:12px;"/>
                    <h:selectOneMenu value="#{SessionBean.ordersFilter.orderStateId}" styleClass="f_s"
                                     style="margin-left:8px;width:200px">
                        <f:selectItems value="#{OrdersBean.orderStates}"/>
                    </h:selectOneMenu>

                    <h:outputText value="Клиент" style="margin-left:12px;"/>
                    <h:panelGroup style="margin-left:17px">
                        <h:inputText value="#{OrdersBean.selectedClientName}" readonly="true"/>
                        <a4j:commandButton value="..."
                                           onclick="Richfaces.showModalPanel('modalClients');return false;"/>
                        <a4j:commandButton value="очистить" onclick="resetClientName();return false;"/>
                    </h:panelGroup>

                    <h:outputText value="Номер в ИС" style="margin-left:12px;"/>
                    <h:inputText value="#{SessionBean.ordersFilter.orderId}"
                                 converterMessage="Неверно задано значение фильтра \"Номер в ИС\""
                                 styleClass="f_tb_l"
                                 style="margin-left:6px;"/>

                </h:panelGrid>
            </h:panelGroup>
            <h:panelGroup styleClass="" layout="block" style="padding-top:4px;padding-left:50px;">
                <a4j:commandButton value="Поиск" reRender="tblOrders, dvSelectedClientInfo"/>
            </h:panelGroup>
        </h:panelGrid>
    </h:form>
</h:panelGroup>

<%-- Действия --%>
<h:form>
    <h:panelGroup id="dvActions" styleClass="" layout="block"
                  style="background:whitesmoke;padding-top:7px;padding-bottom:7px;padding-left:5px;padding-right:5px;">
        <h:outputText value="<b>Действия: </b>" escape="false" style="padding-right:4px"/>
        <a4j:commandLink value="Удалить пустые заказы" ajaxSingle="true" action="#{RemoveEmptyOrdersBean.remove}"
                         reRender="tblOrders"/>
    </h:panelGroup>
</h:form>

<h:panelGroup id="dvSelectedClientInfo">
    <h:panelGroup rendered="#{SessionBean.ordersFilter.clientId != -1}" styleClass="" layout="block"
                  style="background:whitesmoke;padding-top:7px;padding-bottom:7px;padding-left:5px;padding-right:5px;">
        <h:outputText value="Долг по банку: "/>
        <h:outputText value="#{OrdersBean.selectedClient.debtBank}" style="color:blue"/>
        <rich:spacer width="20"/>
        <h:outputText value="Долг по кассе: "/>
        <h:outputText value="#{OrdersBean.selectedClient.debtCash}" style="color:blue"/>
    </h:panelGroup>
</h:panelGroup>

<%-- Таблица заказов --%>
<rich:dataTable id="tblOrders"
                value="#{OrdersBean.ordersList}"
                var="order"
                style="margin:5px"
                rowKeyVar="row"
                columnClasses="ordrs_c0,ordrs_c0,ordrs_c1,ordrs_c2,ordrs_c3,ordrs_c4,ordrs_c5,ordrs_c6,ordrs_c7,ordrs_c8,ordrs_c9,ordrs_c10">
    <f:facet name="header">
        <h:panelGroup>
            <h:outputText value="Заказы" styleClass="table_caption"/>
            <rich:spacer width="10"/>
            <h:commandButton onclick="go('neworder');return false;" value="Создать новый заказ..."/>
        </h:panelGroup>

    </f:facet>
    <rich:column styleClass="#{order.reservationDateIsInPast ? 'delivery':''}">

        <!-- Редактировать заказ -->
        <h:outputLink value="#"
                      onclick="editOrder(#{order.id}, #{order.clientId});">
            <h:graphicImage value="/resources/img/edit.gif"/>
        </h:outputLink>

        <!-- Удалить заказ -->
        <h:outputLink value="#"
                      rendered="#{order.props['deletable'] == 'true'}"
                      onclick="deleteOrder(#{order.id});">
            <h:graphicImage value="/resources/img/delete.gif"/>
        </h:outputLink>

    </rich:column>

    <rich:column styleClass="#{order.reservationDateIsInPast ? 'delivery':''}">
        <f:facet name="header">
            <h:outputText value="№ заказа"/>
        </f:facet>
        <h:outputText value="#{order.id}" style="color:dimgray;font-weight:bold;"/>
    </rich:column>

    <rich:column styleClass="#{order.reservationDateIsInPast ? 'delivery':''}">
        <f:facet name="header">
            <h:outputText value="Дата создания"/>
        </f:facet>
        <h:outputText value="#{order.creationDate}"><f:converter converterId="TimestampConverter"/></h:outputText>
    </rich:column>

    <rich:column styleClass="#{order.reservationDateIsInPast ? 'delivery':''}">
        <f:facet name="header">
            <h:outputText value="Срок резервирования"/>
        </f:facet>
        <h:outputText value="#{order.reservationDate}">
            <f:converter converterId="TimestampConverter" />
        </h:outputText>
    </rich:column>

    <rich:column styleClass="#{order.reservationDateIsInPast ? 'delivery':''}">
        <f:facet name="header">
            <h:outputText value="Дата отгрузки"/>
        </f:facet>
        <h:outputText value="#{order.deliveryDateAsString}"
                      rendered="#{not empty order.deliveryDate}"/>
        <h:outputText value="не указана" style="color:red"
                      rendered="#{empty order.deliveryDate}"/>
    </rich:column>

    <rich:column styleClass="#{order.reservationDateIsInPast ? 'delivery':''}">
        <f:facet name="header">
            <h:outputText value="Стоимость"/>
        </f:facet>
        <h:outputText id="lOrderSum"
                      value="#{order.factSum == 0 ? order.sumAsString : order.factSumAsString}"
                      title="#{order.sum} (заказ) + #{order.deliverySum} (доставка) = #{order.sum + order.deliverySum} (итого)"/>
    </rich:column>

    <rich:column styleClass="#{order.reservationDateIsInPast ? 'delivery':''}">
        <f:facet name="header">
            <h:panelGroup>
                <h:outputText value="Количество"/><br>
                <h:outputText value="позиций"/>
            </h:panelGroup>
        </f:facet>
        <h:outputText value="#{order.positionsCount} шт."/>
    </rich:column>

    <rich:column styleClass="#{order.reservationDateIsInPast ? 'delivery':''}">
        <f:facet name="header">
            <h:outputText value="Статус заказа"/>
        </f:facet>
        <h:outputText value="#{order.orderStateAsString}"/>
    </rich:column>

    <rich:column styleClass="#{order.reservationDateIsInPast ? 'delivery':''}">
        <f:facet name="header">
            <h:outputLink value="#" onclick="go('users')" style="color:black">Менеджер</h:outputLink>
        </f:facet>
        <h:outputText value="#{order.manager.fio}"/>
    </rich:column>

    <rich:column styleClass="#{order.reservationDateIsInPast ? 'delivery':''}">
        <f:facet name="header">
            <h:outputLink value="#" onclick="go('ClientsPage.jsf')" style="color:black">Клиент</h:outputLink>
        </f:facet>
        <h:outputText value="#{order.client.companyName}"/>
    </rich:column>

    <rich:column styleClass="#{order.reservationDateIsInPast ? 'delivery':''}">
        <f:facet name="header">
            <h:outputText value="Номер 1С"/>
        </f:facet>
        <h:outputText value="#{order.c1Num}" rendered="#{not empty order.c1Num}"/>
        <h:outputText value="не установлен" rendered="#{empty order.c1Num}" style="font-size:80%;color:dimgray;"/>
    </rich:column>

    <%--<rich:column>--%>
    <%--<f:facet name="header">--%>
    <%--<h:outputText value="Способ доставки"/>--%>
    <%--</f:facet>--%>
    <%--<h:outputText value="#{order.deliveryTypeAsString}"/>--%>
    <%--</rich:column>--%>

</rich:dataTable>


<rich:modalPanel id="modalClients" autosized="false" width="500" height="600" onshow="$('tbClientName').focus()">
    <f:facet name="header">
        <h:panelGroup>
            <h:outputText value="Поиск клиента"/>
        </h:panelGroup>
    </f:facet>
    <f:facet name="controls">
        <h:graphicImage value="/resources/img/close.png" onclick="Richfaces.hideModalPanel('modalClients');"/>
    </f:facet>
    <a4j:form prependId="false">
        <a4j:jsFunction name="setSelectedClient" ajaxSingle="true" limitToList="true" reRender="dvFilter"
                        oncomplete="Richfaces.hideModalPanel('modalClients');return false;">
            <a4j:actionparam name="clientId" assignTo="#{SessionBean.ordersFilter.clientId}"/>
        </a4j:jsFunction>
    </a4j:form>
    <h:form prependId="false" id="modalClientsForm">
        <h:panelGrid columns="2" cellpadding="5" cellspacing="5" columnClasses="top,top">
            <h:outputText value="Наименование компании: " styleClass="b"/>
            <h:panelGroup>
                <h:inputText value="#{OrdersBean.clientName}" style="width:300px;" id="tbClientName">
                    <a4j:support event="onkeyup" eventsQueue="clientName" reRender="tblClients" status="a4j_status2"/>
                </h:inputText><br>
                <h:outputText value="Необходимо ввести не менее 3х символов"
                              style="color:red;vertical-align:top;font-size:80%"/>
            </h:panelGroup>
        </h:panelGrid>
        <a4j:status forceId="true" id="a4j_status2">
            <f:facet name="start">
                <h:panelGroup layout="block" styleClass="" style="height:20px">
                    <h:outputText value="Ждите... Выполняется фильтрация списка клиентов" style="font-size:80%"/>
                </h:panelGroup>
            </f:facet>
            <f:facet name="stop">
                <rich:spacer height="20px"/>
            </f:facet>
        </a4j:status>
    </h:form>
    <h:panelGroup id="tblClients">
        <h:panelGroup rendered="#{empty OrdersBean.clients and not empty OrdersBean.clientName}">
            <h:outputText value="Ничего не найдено" styleClass="b"/>
        </h:panelGroup>

        <h:panelGroup rendered="#{not empty OrdersBean.clients}">
            <rich:dataTable id="_tblClients" value="#{OrdersBean.clients}" var="client" style="margin-top:15px;"
                            rows="20" styleClass="full">
                <rich:column>
                    <h:outputLink value="#" onclick="setSelectedClient(#{client.id}); return false;">
                        <h:outputText value="#{client.companyName}"/>
                    </h:outputLink>
                    <%--<a4j:commandLink ajaxSingle="true"--%>
                    <%--limitToList="true"--%>
                    <%--reRender="dvFilter"--%>
                    <%--oncomplete="Richfaces.hideModalPanel('modalClients');return false;">--%>
                    <%--<h:outputText value="#{client.companyName}"/>--%>
                    <%--<a4j:actionparam name="client_id" assignTo="#{SessionBean.ordersFilter.clientId}"--%>
                    <%--value="#{client.id}"/>--%>
                    <%--</a4j:commandLink>--%>
                </rich:column>
            </rich:dataTable>
        </h:panelGroup>
    </h:panelGroup>

</rich:modalPanel>


</f:view>
</body>
</html>
